package com.gdpu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gdpu.bean.Leftmenu;
import com.gdpu.bean.TbUser;
import com.gdpu.common.*;
import com.gdpu.service.LeftmenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private LeftmenuService leftmenuService;

    //动态生成左边导航菜单栏，通过生成Json传送到前端页面实现
    @RequestMapping("loadIndexLeftMenuJson")
    public DataGridView loadIndexLeftMenuJson(){
        //查询所有菜单
        QueryWrapper<Leftmenu> queryWrapper = new QueryWrapper<>();
        List<Leftmenu> list = null;
        //获得用户  判断用户的类型
        TbUser tbUser = (TbUser) WebUtils.getSession().getAttribute("user");
        //总仓库管理员拥有菜单
        if(tbUser.getRoleId() == Constast.USER_TYPE_TOTLE){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //超级管理员拥有菜单
        else if(tbUser.getRoleId() == Constast.USER_TYPE_SUPER){
            queryWrapper.notIn("id",Constast.SUPER_USER_One);
            queryWrapper.notIn("id",Constast.SUPER_USER_Two);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //一号仓库管理员
        else if(tbUser.getRoleId() == Constast.USER_TYPE_ONE){
            //根据用户ID+角色+权限去查询
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //2号仓库管理员
        else if(tbUser.getRoleId() == Constast.USER_TYPE_TWO){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //3号仓库管理员
        else if(tbUser.getRoleId() == Constast.USER_TYPE_THREE){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //4号仓库管理员
        else if(tbUser.getRoleId() == Constast.USER_TYPE_FORTH){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //5号仓库管理员
        else if(tbUser.getRoleId() == Constast.USER_TYPE_FIVE){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //供应商
        else if(tbUser.getRoleId() == Constast.USER_TYPE_Provider){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_One);
            queryWrapper.notIn("id",Constast.SUPER_USER_Two);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //普通客户
        else if(tbUser.getRoleId() == Constast.USER_TYPE_User){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_One);
            queryWrapper.notIn("id",Constast.SUPER_USER_Two);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Seven);
            queryWrapper.notIn("id",Constast.SUPER_USER_Eight);
            list = leftmenuService.list(queryWrapper);
        }
        //审核员
        else if(tbUser.getRoleId() == Constast.USER_TYPE_Auditor){
            queryWrapper.notIn("id",Constast.SUPER_USER_OWN);
            queryWrapper.notIn("id",Constast.SUPER_USER_One);
            queryWrapper.notIn("id",Constast.SUPER_USER_Two);
            queryWrapper.notIn("id",Constast.SUPER_USER_Three);
            queryWrapper.notIn("id",Constast.SUPER_USER_Forth);
            queryWrapper.notIn("id",Constast.SUPER_USER_Five);
            queryWrapper.notIn("id",Constast.SUPER_USER_Six);
            list = leftmenuService.list(queryWrapper);
        }

        List<TreeNode> treeNodes = new ArrayList<>();
        for (Leftmenu leftmenu: list) {
            Integer id = leftmenu.getId();
            Integer pid = leftmenu.getPid();
            String title = leftmenu.getTitle();
            String icon = leftmenu.getIcon();
            String href = leftmenu.getHref();
            Boolean spread = leftmenu.getOpen()==Constast.OPEN_TRUE?true:false;
            treeNodes.add(new TreeNode(id,pid,title,icon,href,spread));
        }
        //用获得的菜单信息生成相应的Json
        List<TreeNode> list1 = TreeNodeBuilder.build(treeNodes,1);
        return new DataGridView(list1);
    }


}

